package com.supermarket.manage.business.usermanage.controller;

import com.supermarket.manage.common.dto.CodeStatus;
import com.supermarket.manage.common.dto.ResponseResult;
import com.supermarket.manage.common.dto.ucenter.response.PermissionResponse;
import com.supermarket.manage.provider.api.ucenter.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-01 17:52
 **/
@RestController
@RequestMapping("/permission")
@Api(value = "PermissionController", tags = "权限接口")
public class PermissionController {

    @Reference(version = "1.0.0")
    private PermissionService permissionService;

    @PreAuthorize("hasAnyAuthority('/','/user/get')")
    @GetMapping("/getAllPermission")
    @ApiOperation(value = "权限列表", notes = "权限列表")
    public ResponseResult<List<PermissionResponse>> getAllPermission() {
        List<PermissionResponse> result = permissionService.getAllPermission();
        if (result == null) {
            return new ResponseResult<>(CodeStatus.FAIL, "查询失败");
        }
        return new ResponseResult<>(CodeStatus.OK, "查询成功", result);
    }

    @PreAuthorize("hasAnyAuthority('/','/user/get')")
    @GetMapping("/getPermissionByUserId/{userId}")
    @ApiOperation(value = "根据用户id获取权限列表", notes = "根据用户id获取权限列表")
    public ResponseResult<List<PermissionResponse>> getPermissionByUserId(@NotNull(message = "用户id不能为空")
                                                                          @PathVariable("userId") Long userId) {
        List<PermissionResponse> result = permissionService.getPermissionByUserId(userId);
        if (result == null) {
            return new ResponseResult<>(CodeStatus.FAIL, "查询失败");
        }
        return new ResponseResult<>(CodeStatus.OK, "查询成功", result);
    }

}
